// import { startRecord, endRecord } from './Native';
class AudioMgr {
    _music = 'bgmusic';
    current = 0;
    _music_volume = 1;
    _effect_volume = 1;
    _voice_volume = 1;
    _audioCache = {};
    sayState = false;
    stateVolume = 0;
    /**是否震动 */
    vibrate = true;
    prebackvolume = 0;
    //单例
    private static _ins: AudioMgr;
    public static get ins(): AudioMgr {
        if (this._ins == null) {
            this._ins = new AudioMgr();
        }
        return this._ins;
    }
    constructor() {
        // let music = cc.sys.localStorage.getItem('music');
        // this._music = music ? music : 'bgmusic';
        let mv = cc.sys.localStorage.getItem('music_volume');
        console.log('mv', mv);
        if (mv == 1 || mv == '' || mv == null) {
            console.log('mv111', mv);
            this._music_volume = 1;
        } else {
            console.log('mv222', mv);
            this._music_volume = 0;
        }


        let ev = cc.sys.localStorage.getItem('effect_volume');
        if (ev == 1 || ev == '' || ev == null) {
            this._effect_volume = 1;
        } else {
            this._effect_volume = 0;
        }


        let vo = cc.sys.localStorage.getItem('voice_volume');
        if (vo == 1) {
            this._voice_volume = 1;
        } else {
            this._voice_volume = 0;
        }



        let vibrate = cc.sys.localStorage.getItem('vibrate');
        console.log('读取震动', vibrate);
        if (vibrate == 1 || vibrate == '' || vibrate == null) {

            this.vibrate = true;
        } else {
            this.vibrate = false;
        }
        console.log('读取震动1', this.vibrate);
        console.log(mv, ev, vo);

        console.log(this._music, this._music_volume, this._effect_volume, this.vibrate);

    }
    private play(url: cc.AudioClip, isbgm: boolean) {
        if (isbgm) {
            cc.audioEngine.stop(this.current);
            this.current = cc.audioEngine.play(url, isbgm, this._music_volume);
            return this.current;
        } else {
            return cc.audioEngine.play(url, isbgm, this._effect_volume);
        }
        // }
    }
    private playLoad(path: string, bundle = cc.assetManager.getBundle('hall'), isbgm: boolean) {
        let audio = this._audioCache[path];
        if (audio) {
            return this.play(audio, isbgm);
        } else {
            let self = this;
            bundle.load("audio/" + path, function (err, clip: cc.AudioClip) {
                if (err) {
                    cc.error(err.message || err);
                    return;
                }
                self._audioCache[path] = clip;
                return self.play(clip, isbgm);
            });
        }
    }
    /**使手机发生较短时间的振动（15 ms）。仅在 iPhone `7 / 7 Plus` 以上及 Android 机型生效 */
    public vibrateShort() {
        // /** 需要基础库： `2.13.0`
        //  *
        //  * 震动强度类型，有效值为：heavy、medium、light */
        //  type: string
        //  /** 接口调用结束的回调函数（调用成功、失败都会执行） */
        //  complete?: VibrateShortCompleteCallback
        //  /** 接口调用失败的回调函数 */
        //  fail?: VibrateShortFailCallback
        //  /** 接口调用成功的回调函数 */
        //  success?: VibrateShortSuccessCallback
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            if (this.vibrate) {
                wx.vibrateShort({
                    'type': 'light',

                    'complete': () => {
                        console.log('接口调用结束的回调函数（调用成功、失败都会执行）');
                    },
                    'fail': () => {
                        console.log('接口调用失败的回调函数');
                    },
                    'success': () => {
                        console.log('接口调用成功的回调函数');
                    }
                })
            }
        }

    }


    /**停止播放所有声音 */
    public stopPlay(effect_id: number) {
        cc.audioEngine.stop(effect_id);
    }
    /**播放Bgm */
    public playBgm(path: string, bundle = cc.assetManager.getBundle('hall')) {
        this.playLoad(path, bundle, true);
    }
    /**播放音效片段 */
    public playAudio(path: string, bundle = cc.assetManager.getBundle('hall')) {
        this.playLoad(path, bundle, false);
    }

    public playLocalAudio(path: string) {
        if (!CC_JSB) {
            return false;
        }
        cc.assetManager.loadRemote(path, function (err, clip: cc.AudioClip) {
            if (err) {
                cc.error(err.message || err);
                return false;
            }
            let audio_id = cc.audioEngine.play(clip, false, 1.0);
            if (audio_id <= 0) {
                return false;
            }
            cc.audioEngine.setFinishCallback(audio_id, function () {
                cc.audioEngine.uncache(clip);
            });
        });
        return true;
    }

    /**设置是否震动 */
    public setvibrate(n) {
        let num = n == 1 ? true : false;
        this.vibrate = num;
        console.log('存储震动', num);

        cc.sys.localStorage.setItem('vibrate', n);
    }
    /**获取是否震动 */
    public gevibrate(n) {
        return this.vibrate;
    }
    /**设置音乐音量 */
    public setMusicVolume(n, change = true) {
        this._music_volume = n == 1 ? 1 : 0;
        cc.audioEngine.setVolume(this.current, this._music_volume);
        console.log('存储音乐', n);

        // if (change) {
        cc.sys.localStorage.setItem('music_volume', n);
        // }
    }
    /**获取音乐音量 */
    public getMusicVolume() {
        return this._music_volume;
    }
    /**设置音效音量 */
    public setEffectVolume(n) {
        this._effect_volume = n == 1 ? 1 : 0;
        console.log('存储音效', this._effect_volume);
        cc.sys.localStorage.setItem('effect_volume', n);
    }
    /**获取音效音量 */
    public getEffectVolume() {
        return this._effect_volume;
    }

    /**设置语音音量 */
    public setVoiceVolume(n) {
        this._voice_volume = Math.floor(n * 100) / 100;
        cc.sys.localStorage.setItem('voice_volume', this._voice_volume);
    }
    /**获取语音音量 */
    public getVoiceVolume() {
        return this._voice_volume;
    }
    /**获取当前声音 */
    public getMusic() {
        return this._music;
    }
    /**设置当前声音 */
    public setMusic(n) {
        this._music = n;
        cc.sys.localStorage.setItem('music', this._music);
    }

    /**播放默认背景音乐 */
    public playDefaultBgm() {
        this.playBgm(this._music);
    }
    /**默认按钮音乐 */
    public playDefaultBtn() {
        this.playAudio('BtnClick');
    }

    public playOutCard() {
        this.playAudio('OutPokers');
    }

    public playGetCard() {
        this.playAudio('sendcard');
    }
    public bigenSay() {
        if (!this.sayState) {
            this.stateVolume = this.getMusicVolume();
            this.setMusicVolume(0);
            this.sayState = true;
        }
    }
    public stopSay() {
        if (this.sayState) {
            this.setMusicVolume(this.stateVolume);
            this.sayState = false;
        }
    }
    // public startRecord() {
    //     if (startRecord()) {
    //         this.prebackvolume = this.getMusicVolume();
    //         this.setMusicVolume(0, false);
    //         return true;
    //     } else {
    //         return false;
    //     }
    // }
    // public endRecord() {
    //     if (!CC_JSB) {
    //         return false;
    //     }
    //     this.setMusicVolume(this.prebackvolume, false);
    //     endRecord();
    //     return true;
    // }
}
export default AudioMgr;